// poj1273
// ml:run = $bin < input
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>

int const maxn = 220;
int const maxm = maxn * maxn;
int const inf = 1 << 30;

struct edge
{
    int to;
    int w;
    int next;
};

edge edges[maxm];

int head[maxn], id;

void add_edge(int u, int v, int w)
{
    edges[id].to = v; edges[id].w = w; edges[id].next = head[u]; head[u] = id++;
    edges[id].to = u; edges[id].w = 0; edges[id].next = head[v]; head[v] = id++;
}

int numh[maxn], h[maxn], curedges[maxn], pre[maxn];

void bfs(int end, int n)
{
    memset(numh, 0, sizeof(numh));
    for (int i = 1; i <= n; ++i) numh[h[i]=n]++;
    h[end] = 0;
    numh[n]--;
    numh[0]++;
    std::queue<int> q;
    q.push(end);

    while (!q.empty()) {
        int v = q.front();
        q.pop();
        int i = head[v];
        while (i != -1) {
            int u = edges[i].to;

            if (h[u] < n) {
                i = edges[i].next;
                continue;
            }

            h[u] = h[v] + 1;
            numh[n]--;
            numh[h[u]]++;
            q.push(u);
            i = edges[i].next;
        }
    }
}

int sap(int start, int end, int n)
{
    int curflow, flowans = 0, temp, neck;
    memset(h, 0, sizeof(h));
    memset(pre, -1, sizeof(pre));
    for (int i = 1; i <= n; ++i)
        curedges[i] = head[i];

    bfs(end, n);
    int u = start;
    while (h[start] < n) {
        if (u == end) {
            curflow = inf;
            for (int i = start; i != end; i = edges[curedges[i]].to) {
                if (curflow > edges[curedges[i]].w) {
                    neck = i;
                    curflow = edges[curedges[i]].w;
                }
            }

            for (int i = start; i != end; i = edges[curedges[i]].to) {
                temp = curedges[i];
                edges[temp].w -= curflow;
                edges[temp^1].w += curflow;
            }

            flowans += curflow;
            u = neck;
        }
        int i;
        for (i = curedges[u]; i != -1; i = edges[i].next)
            if (edges[i].w && h[u]==h[edges[i].to]+1) break;

        if (i != -1) {
            curedges[u] = i;
            pre[edges[i].to] = u;
            u = edges[i].to;
        } else {
            if (0 == --numh[h[u]]) break;
            curedges[u] = head[u];
            for (temp = n, i = head[u]; i != -1; i = edges[i].next)
                if (edges[i].w) temp = std::min(temp, h[edges[i].to]);

            h[u] = temp + 1;
            numh[h[u]]++;
            if (u != start) u = pre[u];
        }
    }
    return flowans;
}

int main()
{
    int n, m, u, v, w;
    while (~scanf("%d%d", &n, &m)) {
        memset(head, -1, sizeof(head));
        id = 0;
        for (int i = 0; i < n; ++i) {
            scanf("%d%d%d", &u, &v, &w);
            add_edge(u, v, w);
        }
        printf("%d\n", sap(1, m, m));
    }
}

